<?php
// .-----------------------------------------------------------------------------------
// |  Software: [HDPHP framework]
// |   Version: 2013.01
// |      Site: http://www.hdphp.com
// |-----------------------------------------------------------------------------------
// |    Author: 向军 <houdunwangxj@gmail.com>
// | Copyright (c) 2012-2013, http://houdunwang.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// |   License: http://www.apache.org/licenses/LICENSE-2.0
// '-----------------------------------------------------------------------------------

/**
 * HDPHP模板引擎
 * @package     session
 * @subpackage  driver
 * @author      后盾向军 <houdunwangxj@gmail.com>
 */
final class HdView extends View {

    static public $vars = array(); //模板变量
    public $tplFile; //模版文件
    private $compileFile; //编译文件

    /**
     * 模板显示
     * @param string    $tpl_file       模板文件
     * @param number    $cacheTime      缓存时间
     * @param string    $contentType    文件类型
     * @param string    $charset        字符集
     * @param boolean   $show           是否显示
     */
    public function display($tplFile = "", $cacheTime = null, $contentType = "text/html", $charset = "", $show = true) {
        $this->tplFile = $this->getTemplateFile($tplFile);

        $this->compileFile = PATH_TEMP_COMPILE . '/' . md5_d($this->tplFile) . '.php';
        if (C("debug")) {
            tpl_compile(array(basename($this->tplFile), $this->compileFile)); //记录模板编译文件
        }
        //缓存时间设置
        $_cacheTime = is_null($cacheTime) ? C("CACHE_TPL_TIME") : $cacheTime;
        $cacheTime = is_int($_cacheTime) ? $_cacheTime : Null;
        $content = false;
        if ($cacheTime) {
            $content = S($_SERVER['REQUEST_URI'], false, null, array("dir" => PATH_TEMP_TPL_CACHE, "driver" => "File"));
        }

        if (!$content) {//缓存失效
           
            if ($this->checkCompile($tplFile)) {//编译文件失效
                $this->compile();
            }
           
            $_CONFIG = C();
            $_LANGUAGE = L();
            if (!empty(self::$vars)) {//加载全局变量
                extract(self::$vars);
            }
            ob_start();
            include($this->compileFile);
            $content = ob_get_clean();
            if (!is_null($cacheTime)) {
                is_dir(PATH_TEMP_TPL_CACHE) || dir_create(PATH_TEMP_CACHE); //创建缓存目录
                S($_SERVER['REQUEST_URI'], $content, $cacheTime, array("dir" => PATH_TEMP_TPL_CACHE, "driver" => "File")); //写入缓存
            }
        }
        if ($show) {
            $charset = strtoupper(C("CHARSET")) == 'UTF8' ? "UTF-8" : strtoupper(C("CHARSET"));
            if (!headers_sent()) {
                header("Content-type:" . $contentType . ';charset=' . $charset);
            }
            echo $content;
        } else {
            return $content;
        }
    }

    /**
     * 获得视图内容
     */
    public function fetch($tplFile = "", $cacheTime = null, $contentType = "text/html", $charset = "") {
        return $this->display($tplFile, $cacheTime, $contentType, $charset, false);
    }

    /**
     * 验证缓存是否过期
     * @param number    $cacheTime  缓存时间
     * @return boolean
     */
    public function isCache() {
        return S($_SERVER['REQUEST_URI'], false, null, array("dir" => PATH_TEMP_TPL_CACHE, "driver" => "File")) ? true : false;
    }

    /**
     * 验证编译文件是否过期  包括时间验证  是否开启调试
     * @param type $tplFile             模版文件
     * @param type $compileFile     编译文件
     */
    private function checkCompile() {
        $tplFile = $this->tplFile;
        $compileFile = $this->compileFile;
        return C("DEBUG") || C("ALWAYS_COMPILE_TPL") || //开启调试或持久编译
                !COMPILE || //不编译
                !file_exists($compileFile) || //模板不存在
                (filemtime($tplFile) > filemtime($compileFile)); //编板有更新
    }

    /**
     * 解析模版 +创建编译文件
     * @param type $tplFile
     * @param type $compileFile
     */
    public function compile() {
        if (!$this->checkCompile())
            return;
        load_file(PATH_HD . '/libs/driver/view/hd/Compile.class.php');
        $assignVar = array_keys(self::$vars); //assign分配的变量
        
        $compileObj = new compile($this->tplFile, $this->compileFile, $assignVar);
        $compileObj->run();
        
    }

    /**
     * 获得编译文件内容
     * @return String
     */
    public function getCompileContent() {
        return file_get_contents($this->compileFile);
    }

    /**
     * 向模板中传入变量
     * @param type $var     变量名
     * @param type $value   变量值
     */
    public function assign($var, $value) {
        return self::$vars[$var] = $value; //传入变量
    }
    /**
     * 设置ASSIGN分配的变量
     * @param string $name    变量名
     * @param mixed $value   值
     */
    function __set($name, $value) {
        if (isset(self::$vars[$name])) {
            self::$vars[$name] = $value;
        }
    }

}

?>